<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="开户名" prop="bankAccName" label-width="90px">
        <el-input
          v-model="queryParams.bankAccName"
          placeholder="请输入开户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="cardNo" label-width="90px">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="open = true"
          v-hasPermi="['account:transferAccount:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['account:transferAccount:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferAccountList">
      <el-table-column label="开户名" min-width="180" align="center" prop="bankAccName" />
      <el-table-column label="卡号" align="center" prop="cardNo" min-width="150" />
      <el-table-column label="可用余额" align="center" prop="availableAmount" min-width="100">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.isShow" @click="handleAccountBookQuery(scope.row)" size="mini" type="text" icon="el-icon-view"></el-button>
          <span  v-else style="cursor: pointer" @click="scope.row.isShow = false">{{ scope.row.availableAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来账订阅" align="center" prop="isDefault" min-width="100">
        <template slot-scope="{ row }">
          <template v-if="row.isSubscribe === 0">
            <el-button
              size="mini"
              type="text"
              @click="handleAccountBookSubscribe(row)"
              v-hasPermi="['ecs:aliTransferAccount:edit']"
            >立即开通</el-button>
          </template>
          <span v-else>正常</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.productId === 1"
            icon="el-icon-edit"
            @click="handleBack(scope.row)"
            v-hasPermi="['account:transferAccount:edit']"
          >编辑</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="handleDetail(scope.row)"
            v-hasPermi="['account:transferAccount:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="账户类型" :visible.sync="open" width="500px" append-to-body destroy-on-close>
      <template>
        <el-card shadow="hover" class="account-card" v-for="(item,index) in accountBack" :key="index" @click.native="item.productId === 1?handleAdd(item):handleBack(item)">
          <span>{{ item.productName }}</span>
        </el-card>
      </template>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="!form.transferAccountId?'添加'+title:'编辑'+title" @close="handleBackClose" :visible.sync="backOpen" width="800px" append-to-body destroy-on-close>
      <transfer-account-operate :transferAccountId="form.transferAccountId" @close="backOpen=false" :productId="form.productId"></transfer-account-operate>
    </el-dialog>

  </div>

</template>

<script>
import { listProductOrganizations } from "@/api/product/productOrganizations"
import TransferAccountOperate from "@/views/transfer/transferAccount/profile/operate.vue";
import { listTransferAccount, getTransferAccount, delTransferAccount, accountBookSubscribe, accountBookQuery } from "@/api/transfer/transferAccount"

export default {
  name: "TransferAccount",
  components: {TransferAccountOperate},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出款账户表格数据
      accountBack:[],
      transferAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      backOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardNo: null,
        bankAccName: null,
        organizationId: null,
      },
      // 表单参数
      form: {
        transferAccountId:null
      },
    }
  },
  created() {
    listProductOrganizations().then(res=>{
      this.accountBack = res.rows
    })
    this.getList()
  },
  methods: {
    /** 查询出款账户列表 */
    getList() {
      this.loading = true
      listTransferAccount(this.queryParams).then(response => {
        this.transferAccountList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加出款账户"
    },

    /** 新增按钮操作 */
    handleBack(row){
      this.backOpen = true
      if(!row.transferAccountId){
        this.title = row.productName
      }else {
        this.title = row.bankAccName
        this.form.transferAccountId = row.transferAccountId
      }
      this.form.productId = row.productId
    },


    handleDetail(row){
      this.detailOpen = true
      this.form.transferAccountId = row.transferAccountId
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const transferAccountId = row.transferAccountId
      getTransferAccount(transferAccountId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改出款账户"
      })
    },

    handleAccountBookSubscribe(row) {
      const configIds = row.transferAccountId;
      this.$modal.confirm('是否确认要订阅协议号 "' +row.agreementNo + '" 的出款账户？').then(function() {
        return accountBookSubscribe(configIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("订阅成功");
      }).catch(() => {});
    },


    handleBackClose(){
      this.form.transferAccountId  = null
      this.form.productId = null;
      this.getList();
    },

    handleAccountBookQuery(row){
      this.loading = true;
      accountBookQuery(row.transferAccountId).then(res=>{
        this.loading = false;
        this.$set(row, 'availableAmount', res.data.availableAmount);
        this.$set(row, 'isShow', true);
      }).catch(()=>{
        this.loading = false;
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除出款账户编号为"' + row.transferAccountId + '"的数据项？').then(function() {
        return delTransferAccount(row.transferAccountId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('account/transferAccount/export', {
        ...this.queryParams
      }, `transferAccount_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>


<style scoped lang="scss">
.account-card{
  margin-bottom: 5px;
}
::v-deep .el-dialog__body{
  padding-top: 10px;
}
</style>

